from django.shortcuts import render, redirect

from la import models
from la.views import StoreInfoModelForm


def storeinfo(req):
    '''店铺列表'''
    storeinfos = models.storeinfo.objects.filter(isopen = 1).all()
    return render(req, "storeinfo.html",{"storeinfo":storeinfos})

def storeinfo_add(req):
    '''店铺添加'''
    if req.method == "GET":
        form = StoreInfoModelForm()
        return render(req,"storeinfo_add.html",{"form":form})
    form = StoreInfoModelForm(data=req.POST)
    if form.is_valid():
        form.instance.isopen = 1
        form.save()
        return redirect("/la/storeinfo/")
    else:
        return render(req,"storeinfo_add.html",{"form":form})

def storeinfo_edit(req, nid):
    '''店铺修改'''
    storeinfo = models.storeinfo.objects.filter(id=nid).first()
    if req.method == "GET":
        form = StoreInfoModelForm(instance=storeinfo)
        return render(req,"storeinfo_edit.html",{"form":form})
    form = StoreInfoModelForm(data=req.POST,instance=storeinfo)
    if form.is_valid():
        form.instance.isopen = 1
        form.save()
        return redirect("/la/storeinfo/")
    else:
        return render(req,"storeinfo_edit.html",{"form":form})

def storeinfo_stop(req,nid):
    '''店铺停用'''
    models.storeinfo.objects.filter(id=nid).update(isopen=0)
    return redirect("/la/storeinfo/")